package com.xiaochen.baidu.common.design.strategy;

import com.xiaochen.baidu.service.IMoiuLoginService;
import com.xiaochen.baidu.service.IMoiuParseService;
import com.xiaochen.baidu.service.IRedisService;

import javax.annotation.Resource;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author xc
 * @date 2025/10/11/19:39
 * @desc
 */
public abstract class AbstractMultiThreadStrategyRouter<T, D, R> implements StrategyMapper<T, D, R>,StrategyHandler<T, D, R> {

    @Resource
    protected IRedisService redisService;
    @Resource
    protected IMoiuLoginService moiuLoginService;
    @Resource
    protected IMoiuParseService moiuParseService;
    @Resource
    protected ThreadPoolExecutor threadPoolExecutor;
    
    protected abstract void multiThread(T requestParameter, D dynamicContext) throws Exception;
    
    protected abstract R apply(T requestParameter, D dynamicContext) throws Exception;
    
    public R route(T requestParameter, D dynamicContext) throws Exception {
        StrategyHandler<T, D, R> strategyHandler = map(requestParameter, dynamicContext);
        return null == strategyHandler ? null : strategyHandler.handle(requestParameter, dynamicContext);
    }
    
    @Override
    public R handle(T requestParameter, D dynamicContext) throws Exception {
        multiThread(requestParameter, dynamicContext);
        return apply(requestParameter, dynamicContext);
    }
}
